﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.Settings;

namespace THANHTUNGO.Portal.Masters
{
    public partial class Office : MasterPage
    {
        protected string LogoffLink = "";
        public bool ShowTabs = true;
        public int tabIndex;

        protected override void OnInit(EventArgs e)
        {
            // Obtain PortalSettings from Current Context
            PortalSettings portalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
            if (portalSettings.ActiveTab.ShowLeftPane)
            {
                leftColumn.Attributes.Add("class", "MainMenu");
                //contentPane.Attributes.Add("class", "MainMenu");
            }
            if (portalSettings.ActiveTab.ShowRightPane)
            {
                rightColumn.Attributes.Add("class", "MainMenu");
                //contentPane.Attributes.Add("class", "MainMenu");
            }
            leftColumn.Attributes.Add("class", "MainMenu");
            contentPane.Attributes.Add("class", "");

            if (portalSettings.ActiveTab.ShowLeftPane)
            this.Head.Title = portalSettings.Name;
            // Dynamically Populate the Portal Site Name
            //siteName.Text = portalSettings.PortalName;

            // If user logged in, customize welcome message
            if (Request.IsAuthenticated)
            {
                WelcomeMessage.Text = "<a href='" + Global.GetApplicationPath(Request) + "/Default.aspx?cid=ChangePassword' class='SiteLink'>" + Global.RM.GetString("SYSTEM_WELCOME") + " " + PortalSecurity.CurrentUser.Identity.Email + "</a> <span>|</span>";
                WelcomeMessage.ToolTip = PortalSecurity.CurrentUser.Identity.ToString();
                

                // if authentication mode is Cookie, provide a logoff link
                if (Context.User.Identity.AuthenticationType == "Forms")
                {
                    LogoffLink = "<a href='" + Global.GetApplicationPath(Request) + "/Admin/Logoff.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_LOGOUT") + "</a>";
                }
            }
            else
            {
                // Allow register?
                if (portalSettings.AllowRegister)
                {
                    LogoffLink = "<a href='" + Global.GetApplicationPath(Request) + "/Register.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_REGISTER") + "</a> <span>|</span> " + "<a href='" + Global.GetApplicationPath(Request) + "/SignIn.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_SIGNIN") + "</a>";
                }
                else
                {
                    LogoffLink = "<a href='" + Global.GetApplicationPath(Request) + "/SignIn.aspx' class=SiteLink> " + Global.RM.GetString("SYSTEM_SIGNIN") + "</a>";
                }
            }

            // Dynamically render portal tab strip
            if (ShowTabs)
            {
                tabIndex = portalSettings.ActiveTab.Index;

                // Build list of tabs to be shown to user                                   
                ArrayList authorizedTabs = new ArrayList();
                int addedTabs = 0;

                for (int i = 0; i < portalSettings.Tabs.Count; i++)
                {
                    Tab tab = (Tab)portalSettings.Tabs[i];

                    if (PortalSecurity.IsInRoles(tab.AuthorizedRoles))
                    {
                        if (tab.ShowOnMenu)
                            authorizedTabs.Add(tab);
                    }

                    if (addedTabs == tabIndex)
                    {
                        tabs.SelectedIndex = addedTabs;
                    }

                    addedTabs++;
                }

                // Populate Tab List at Top of the Page with authorized tabs
                tabs.DataSource = authorizedTabs;
                tabs.DataBind();
            }            
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}